﻿using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using ValidateCommon;
namespace RichWebServiceDll
{
    public class RichWSComputerInfo
    {
        #region Property

        /// <summary>
        /// 请求状态
        /// </summary>
        public string RequestResultState { get; set; }

        /// <summary>
        /// 信息描述
        /// </summary>
        public string RequestResultMessage { get; set; }

        /// <summary>
        /// serverIP
        /// </summary>
        public string ServerIp { get; set; }

        /// <summary>
        /// 全款
        /// </summary>
        public int IsAllMoney { get; set; }

        /// <summary>
        /// 到期日期
        /// </summary>
        public DateTime WebEndTime { get; set; }

        /// <summary>
        /// 序列号
        /// </summary>
        public int MatchImpowerNo { get; set; }

        /// <summary>
        /// 试用版
        /// </summary>
        public int IsTryUse { get; set; }

        /// <summary>
        /// 放弃绑定
        /// </summary>
        public int IsDrop { get; set; }

        /// <summary>
        /// 可用空间
        /// </summary>
        public int CanUseDisk { get; set; }

        #endregion Property

        
        /// <summary>
        ///
        /// </summary>
        /// <param name="strServerIp"></param>
        /// <param name="strWebSiteUrl"></param>
        /// <param name="strImpowerNo">验证码</param>
        public RichWSComputerInfo(string strServerIp, string strWebSiteUrl, string strImpowerNo)
        {
            ServerIp = "0.0.0.0";
            CanUseDisk = 0;
            try
            {
                var builderWebServiceAddress = new StringBuilder();

                var strName = EncryptUtily.EncryptUtily.LoginEncrypt("CMSValidate");
                var strPsd = EncryptUtily.EncryptUtily.LoginEncrypt("Validate2012.JsbRich");
                builderWebServiceAddress.Append("http://" + strServerIp + "/IWebSewvices/WebSiteDataHandleClient.asmx/ResponseClientMessage");
                builderWebServiceAddress.Append("?chvWebSiteUrl=" + EncryptUtily.EncryptUtily.LoginEncrypt(strWebSiteUrl));
                builderWebServiceAddress.Append("&chvImpowerNo=" + UTF8code.RepleaseDES(strImpowerNo));
                builderWebServiceAddress.Append("&strValidateName=" + strName);
                builderWebServiceAddress.Append("&strValidatePsd=" + strPsd);
                var webrequest = WebRequest.Create(builderWebServiceAddress.ToString());
                var webresponse = webrequest.GetResponse();
                var stream = webresponse.GetResponseStream();
                if (stream != null)
                {
                    var sr = new StreamReader(stream, Encoding.UTF8);
                    //读取返回流
                    var xd = new XmlDocument();
                    xd.Load(sr);
                    var strresponseString = xd.ChildNodes[1].InnerText.ToString(CultureInfo.InvariantCulture);
                    var resultHashtable = AnalysisWebResponse(strresponseString);
                    RequestResultState = resultHashtable["RequestResultState"].ToString();
                    RequestResultMessage = resultHashtable["RequestResultMessage"].ToString();
                    ServerIp = resultHashtable["ServerIp"].ToString();
                    IsAllMoney = Convert.ToInt32(resultHashtable["IsAllMoney"]);
                    WebEndTime = Convert.ToDateTime(resultHashtable["WebEndTime"]);
                    MatchImpowerNo = Convert.ToInt32(resultHashtable["MatchImpowerNo"]);
                    IsTryUse = Convert.ToInt32(resultHashtable["IsTryUse"]);
                    IsDrop = Convert.ToInt32(resultHashtable["IsDrop"]);
                    CanUseDisk = Convert.ToInt32(resultHashtable["CanUseDisk"]);
                }
                else
                {
                    RequestResultState = "error";
                    RequestResultMessage = "未在服务器上获取数据";
                }
            }
            catch (Exception ex)
            {
                throw new Exception("获取服务器验证时数据异常:" + ex.Message.Replace("\r\n", ""));
            }
        }

        /// <summary>
        /// 处理返回字符数据并进行分割
        /// </summary>
        /// <param name="strWebResponse">webservice响应</param>
        /// <returns>分割后的哈希值对</returns>
        private Hashtable AnalysisWebResponse(string strWebResponse)
        {
            strWebResponse = EncryptUtily.EncryptUtily.LoginDecrypt(strWebResponse);
            var responseResult = strWebResponse.Split('_');

            var returnResult = new Hashtable();
            returnResult["RequestResultState"] = responseResult[0];
            returnResult["RequestResultMessage"] = responseResult[1];
            returnResult["ServerIp"] = responseResult[2];
            returnResult["IsAllMoney"] = responseResult[3];
            returnResult["WebEndTime"] = responseResult[4];
            returnResult["MatchImpowerNo"] = responseResult[5];
            returnResult["IsTryUse"] = responseResult[6];
            returnResult["IsDrop"] = responseResult[7];
            returnResult["CanUseDisk"] = responseResult[8];

            return returnResult;
        }
    }
}
